import { Injectable } from '@angular/core';
import { Http, URLSearchParams } from '@angular/http';

import 'rxjs/add/operator/toPromise';

import { urlConfigs } from './config';
import { BusinessDetail } from '../model/business-detail';

@Injectable()
export class BusinessService{
	constructor(private http: Http){}

    // 获取列表
    list(pagenum: string, pagesize:string, search:string): Promise<any> {
        let params: URLSearchParams = new URLSearchParams();
        params.set('pagenum', pagenum);
        params.set('pagesize', pagesize);
        params.set('search', search);

        return this.http.get(urlConfigs.business,{search: params})
            .toPromise()
            .then(response => response.json().data)
            .catch(this.handleError);
    };

    // 获取详情
    listDetail(id:string): Promise<any>{
        return this.http.get(urlConfigs.detail+id)
            .toPromise()
            .then(response => response.json().data as BusinessDetail)
            .catch(this.handleError);
    };

    // 处理请求出错
    handleError(error: any): Promise<any> {
        alert('程序请求出错');
        return Promise.reject(error.message || error);
    };
}